<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="模型ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="模型ID"
          clearable
          size="small"
          @input="handleQuery"
        />
      </el-form-item>

      <el-form-item label="上传用户" prop="displayName">
        <el-input
          v-model="queryParams.displayName"
          placeholder="支持搜索发表用户和被评论的用户"
          clearable
          size="small"
          @input="handleQuery"
        />
      </el-form-item>

      <el-form-item label="上传时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="分级状态" prop="isRestricted">
        <el-select
          v-model="queryParams.isRestricted"
          placeholder="分级状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in is_restricted"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="modelStatus">
        <el-select
          v-model="queryParams.modelStatus"
          placeholder="状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in model_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="layout-content">
      <el-row class="table-header">
        <span>模型管理</span>
        <div class="right">
          <el-button type="default" size="mini" @click="addSensitiveWord"
            >下载</el-button
          >
          <el-button type="default" size="mini" @click="addSensitiveWord"
            >分级</el-button
          >
        </div>
      </el-row>

      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="
          tableData.filter(
            (data) =>
              !search || data.name.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="操作" align="left" width="200">
          <template #default="scope">
            <el-button size="mini" type="text" @click="Offline(scope.row)"
              >查看</el-button
            >
            <el-button size="mini" type="text" @click="Offline(scope.row)"
              >下载</el-button
            >

            <template v-if="scope.row.status === 1">
              <el-button
                size="mini"
                type="text"
                @click="changeStatus(scope.row)"
              >
                上线
              </el-button>
            </template>

            <template v-if="scope.row.status === 2">
              <el-button
                size="mini"
                type="text"
                @click="changeStatus(scope.row)"
              >
                下线
              </el-button>
            </template>
          </template>
        </el-table-column>

        <el-table-column label="模型ID" prop="id" width="100px" />
        <el-table-column label="上传用户" prop="user.username" width="150px" />
        <el-table-column
          label="查看次数"
          prop="viewNum"
          width="150px"
          sortable
        />
        <el-table-column
          label="分享次数"
          prop="shareNum"
          width="150px"
          sortable
        />
        <el-table-column
          label="嵌入外部次数"
          prop="embedNum"
          width="150px"
          sortable
        />
        <el-table-column
          label="浏览次数"
          prop="embedViewNum"
          width="150px"
          sortable
        />
        <el-table-column label="状态" prop="status" width="150px">
          <template #default="scope">
            <template v-if="scope.row.status === 1">
              <el-button type="danger" size="small">下线</el-button>
            </template>
            <template v-if="scope.row.status === 2">
              <el-button type="success" size="small">上线</el-button>
            </template>
          </template>
        </el-table-column>

        <el-table-column label="上传时间" prop="createTime" sortable>
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.current"
        v-model:limit="queryParams.size"
        @pagination="getList"
      />
    </div>
  </div>
</template>
<script setup name="UserList">
import { listModel, publishModel } from "@/api/model/list";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const search = ref("");
const total = ref("0");
const loading = ref(true);
const router = useRouter();
const dateRange = ref([]);
const ids = ref([]);
const { is_restricted, model_status } = proxy.useDict(
  "is_restricted",
  "model_status"
);

// 搜索栏的显隐
const showSearch = ref(true);

// 获取用户的选中
const multipleSelection = ref("");
const queryParams = reactive({
  current: 1,
  size: 10,
  id: undefined,
  displayName: undefined,
  isRestricted: undefined,
  status: undefined,
});

// 用户数据列表
const tableData = ref([]);
// 获取用户数据列表
function getList() {
  loading.value = true;
  listModel(queryParams).then((response) => {
    total.value = response.data.total;
    tableData.value = response.data.records;
    loading.value = false;
  });
}

/** 删除按钮操作 */
function Offline(row) {
  const userIds = row.userId || ids.value;
  proxy.$modal
    .confirm("确定下线该评论嘛？")
    .then(function () {
      return delUser(userIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 改变模型的状态
function changeStatus(row) {
  const modelIds = row.userId || ids.value;
  console.log(modelIds);
  // 下线的状态运行的函数
  if (row.status == 2) {
  }

  // 上线的状态运行的函数
  if (row.status == 1) {
    proxy.$modal
      .confirm("确认上线该模型吗？")
      .then(function () {
        return publishModel(modelIds);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("下线成功");
      })
      .catch(() => {});
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.current = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  getList();
}

getList();
</script>
<style lang="scss"></style>
